package tool;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import tool.bean.SysArgs;

public class ClientTool {
	
	/** 客户姓名模糊  电话模糊工具 将这部分数据放到clientBase表中
	 * @param args
	 */

	
	static public String vagueName(String name){
		return name.trim().substring(0,1)+"**";
	}
	
	static public String vaguePhone(String phone){
		
		return phone.trim().substring(0,3)+"*****"+phone.trim().substring(8,11);
	}
	
	//验证是否是姓名 0：OK  1：请检查是否有非法字符  2：请检查名字是否包含称谓
	/**返回值：0-通过  1：非汉字  2：含有称呼  3:IO异常
	 * @return
	 * @throws Exception 
	 */
	public static int isName(String name) throws Exception{
		
	try{	
		boolean isHanzi = false; // 是否汉字
		boolean isMingan = false; // 是否敏感
		String newName = name.replaceAll(" ", "").trim(); // 去中间及两边空格
		String reg = "[\\u4e00-\\u9fa5]{2,}";// 表示+表示一个或多个中文   {2,}两个以上汉字
		isHanzi = newName.matches(reg);
		if(isHanzi==false){
			return 1;
		}
//		// String mgc="先生,老板,女士,某某"; //敏感词过滤 不要硬编码 forbidName.properties
//		Resource rs = new ClassPathResource("bean-config.xml");
//		BeanFactory factory = new XmlBeanFactory(rs);
		String forbidName = SysArgsTool.getSysArgs().getRegulars().get("name");
		String[] forbid = forbidName.split("/");
//		String[] forbid = "先生/老板/司机/经理/有限公司/公司/机手/女士/师傅/客户/小姐/老师".split("/");

		for (String s : forbid) {
			if(newName.indexOf(s)!=-1)
				return 2;
		}
		return 0;
	}catch(Exception e){
		e.printStackTrace();
		throw new Exception();
		
	}
		
	}
	
	//data.setClientCode("CN_"+new Integer(ad.getActivity().getCid()*2+3).toString());
	@Deprecated
	public static String enCodeCid(Integer cid){

		return "KH_"+new Integer(cid*2+3).toString();
	}
	
	@Deprecated
	public static Integer deCodeCid(String cidCode){
		return (new Integer(cidCode.substring(3))-3)/2;
	}
	
	@Deprecated
	public static String encryptEnCodeCid(Integer cid){

		return Base64Tool.encode("KH_"+new Integer(cid*2+3).toString());
	}
	
	@Deprecated
	public static Integer encryptDeCodeCid(String cidCode){
		return (new Integer((Base64Tool.decode(cidCode)).substring(3))-3)/2;
	}
	
	
	
	//验证是否是是手机号码
	public static boolean isMobileNO(String mobile){
//		Pattern p = Pattern.compile(SysArgsTool.getSysArgs().getRegexPhone()); 
		String rePhone=SysArgsTool.getSysArgs().getRegulars().get("phone");
//		System.out.println(rePhone);
		Pattern p = Pattern.compile(rePhone);
//		System.out.println(SysArgsTool.getSysArgs().getRegexPhone());
		
        Matcher m = p.matcher(mobile);       
        //logger.info(m.matches()+"---");       
        return m.matches();  
		
	}
	
	public static void main(String[] args) throws IOException  {
		// TODO Auto-generated method stub
		System.out.println(ClientTool.enCodeCid(15));
		System.out.println(ClientTool.deCodeCid("CN_33"));
		
		System.out.println(ClientTool.encryptEnCodeCid(15));
		System.out.println(ClientTool.encryptDeCodeCid("Q05fMzM="));
		
//		System.out.println(ClientTool.isName("张张张张张"));
//		System.out.println(ClientTool.vaguePhone("17782076532"));
//		System.out.println(ClientTool.isMobileNO("1818207951e"));
//		System.out.println(ClientTool.isName("师"));
//		System.out.println(SysArgsTool.getSysArgs().getForbidname());
//		System.out.println(SysArgsTool.getSysArgs().getRegexPhone());

		
	}

}
